package com.woniuxy.service.impl;

import com.woniuxy.DTO.AddCageDTO;
import com.woniuxy.DTO.GoDownAnimalDTO;
import com.woniuxy.domain.AlgsCage;
import com.woniuxy.domain.AlgsType;
import com.woniuxy.mapper.AlgsCageMapper;
import com.woniuxy.service.IAlgsCageService;
import com.woniuxy.vo.AlgsCageSizeVO;
import com.woniuxy.vo.AlgsCageVO;
import com.woniuxy.vo.IAlgsSelectCageBy2ByCageSizeIdVO;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;


/**
 * @author ：张宇
 * @date ：Created in 2023-03-20 19:07
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class IAlgsCageServiceImpl implements IAlgsCageService {

    @Resource
    private AlgsCageMapper algsCageMapper;
    @Override
    public List<AlgsCageSizeVO> selectAllCage() {
        return algsCageMapper.selectAllCage();
    }

    @Override
    public int goDown(GoDownAnimalDTO goDownAnimalDTO) {
        Integer cageSizeId = goDownAnimalDTO.getCageSizeId();
        List<AlgsCage> algsCages = algsCageMapper.selectByCageSizeIdAndTypeId(cageSizeId.longValue());
        String num = goDownAnimalDTO.getNum();
        int GoDownNum = Integer.parseInt(num);
        if(algsCages.size()>=GoDownNum){
            System.out.println(algsCages.size());
            for(int i = 0; i<GoDownNum;i++){
                AlgsCage algsCage = algsCages.get(i);
                algsCage.setCageBy1(goDownAnimalDTO.getAnimalName());
                algsCage.setCageBy3((new Random().nextInt(10000)+""));

                algsCage.setCageTypeId(71l);
                algsCageMapper.updateByPrimaryKeySelective(algsCage);

            }

            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public AlgsCage selectByPrimaryKey(Long id) {
        return algsCageMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AlgsCage> selectByCageSizeIdAndTypeId(Long cageSizeId) {
        return algsCageMapper.selectByCageSizeIdAndTypeId(cageSizeId);
    }
    /**
     * 查询笼子（库存）
     *
     * @param cageId 笼子（库存）主键
     * @return 笼子（库存）
     */
    @Override
    public AlgsCage selectAlgsCageByCageId(Long cageId)
    {
        return algsCageMapper.selectAlgsCageByCageId(cageId);
    }

    /**
     * 查询笼子（库存）列表
     *
     * @param algsCage 笼子（库存）
     * @return 笼子（库存）
     */
    @Override
    public List<AlgsCage> selectAlgsCageList(AlgsCage algsCage)
    {
        return algsCageMapper.selectAlgsCageList(algsCage);
    }

    /**
     * 新增笼子（库存）
     *
     * @param algsCage 笼子（库存）
     * @return 结果
     */
    @Override
    public int insertAlgsCage(AlgsCage algsCage)
    {
        return algsCageMapper.insertAlgsCage(algsCage);
    }

    /**
     * 修改笼子（库存）
     *
     * @param algsCage 笼子（库存）
     * @return 结果
     */
    @Override
    public int updateAlgsCage(AlgsCage algsCage)
    {
        return algsCageMapper.updateAlgsCage(algsCage);
    }

    /**
     * 批量删除笼子（库存）
     *
     * @param cageIds 需要删除的笼子（库存）主键
     * @return 结果
     */
    @Override
    public int deleteAlgsCageByCageIds(Long[] cageIds)
    {
        return algsCageMapper.deleteAlgsCageByCageIds(cageIds);
    }

    /**
     * 删除笼子（库存）信息
     *
     * @param cageId 笼子（库存）主键
     * @return 结果
     */
    @Override
    public int deleteAlgsCageByCageId(Long cageId)
    {
        return algsCageMapper.deleteAlgsCageByCageId(cageId);
    }

    @Override
    public List<AlgsType> selectAllCageType() {
        return algsCageMapper.selectAllCageType();
    }

    @Override
    public List<AlgsCageVO> selectAllCageList(AlgsCage algsCage) {
        return algsCageMapper.selectAllCageList(algsCage);
    }

    @Override
    public int addCage(AddCageDTO addCageDTO) {
        long l = Long.parseLong(addCageDTO.getCageSizeId());

        int num = Integer.parseInt(addCageDTO.getNum());
        List<IAlgsSelectCageBy2ByCageSizeIdVO> list = algsCageMapper.selectCageBy2ByCageSizeId(l);
        IAlgsSelectCageBy2ByCageSizeIdVO lastCageby2 = list.get(list.size() - 1);
        long lastCageBy2Num = Long.parseLong(lastCageby2.getCageBy2());
        if(Integer.parseInt(addCageDTO.getCageSizeId())==1){
            for (int j = 1;j<=num;j++){
                AlgsCage algsCage = new AlgsCage();
                algsCage.setCageDate(new Date());
                algsCage.setCageSizeId(l);
                algsCage.setCageBy2((lastCageBy2Num+j)+"");
                algsCage.setCageTypeId(72l);
                algsCageMapper.insertAlgsCage(algsCage);
            }
        }else if (Integer.parseInt(addCageDTO.getCageSizeId())==2){
            for (int j = 1;j<=num;j++){
                AlgsCage algsCage = new AlgsCage();
                algsCage.setCageDate(new Date());
                algsCage.setCageSizeId(l);
                algsCage.setCageBy2((lastCageBy2Num+j)+"");
                algsCage.setCageTypeId(73l);
                algsCageMapper.insertAlgsCage(algsCage);
            }
        }else if(Integer.parseInt(addCageDTO.getCageSizeId())==3){
            for (int j = 1;j<=num;j++){
                AlgsCage algsCage = new AlgsCage();
                algsCage.setCageDate(new Date());
                algsCage.setCageSizeId(l);
                algsCage.setCageBy2((lastCageBy2Num+j)+"");
                algsCage.setCageTypeId(76l);
                algsCageMapper.insertAlgsCage(algsCage);
            }
        }

        return 1;
    }

    @Override
    public List<IAlgsSelectCageBy2ByCageSizeIdVO> selectCageBy2ByCageSizeId(Long cageSizeId) {
        return algsCageMapper.selectCageBy2ByCageSizeId(cageSizeId);
    }


}
